package org.example.controller;

import org.example.model.Major;
import org.example.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ ClassName: StudentController
 * @ Description：请求服务端Student的接口
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 16:24 2022/4/25
 */

@Controller
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/name")
    public @ResponseBody
    List<Student> name(HttpServletRequest request) {
        String name = request.getParameter("name");
        List<Student> forObject = restTemplate.getForObject("http://EUREKA-SERVICE/student/name?name=" + name, List.class);
        return forObject;
    }

    @RequestMapping(value = "/id")
    public @ResponseBody
    Student id(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Student forObject = restTemplate.getForObject("http://EUREKA-SERVICE/student/id?id=" + id, Student.class);
        return forObject;
    }

    @RequestMapping(value = "/number")
    public @ResponseBody
    Student number(HttpServletRequest request) {
        String number = request.getParameter("number");
        Student forObject = restTemplate.getForObject("http://EUREKA-SERVICE/student/number?number=" + number, Student.class);
        return forObject;
    }

    @RequestMapping(value = "/all")
    public @ResponseBody
    List<Student> all() {
        List<Student> list = restTemplate.getForObject("http://EUREKA-SERVICE/student/all", List.class);
        return list;
    }

    @RequestMapping(value = "/excelinsert")
    public @ResponseBody
    /*
     * will not be used
     * @param file
     * @return java.lang.String
     * @author LiYing
     * @creed: Talk is cheap,show me the code
     * @date 2022/4/25 16:41
     */
    String ExcelParse(@RequestParam("file") MultipartFile file) {
        File file1 = null;
        try {
            HttpEntity<MultiValueMap<String, Object>> httpEntity;
            String responseEntity = restTemplate.getForObject("http://EUREKA-SERVICE/database/importExcel", String.class);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != file) {
                //最后要删除
                file1.delete();
            }
        }
        return "result";
    }
}
